package com.banksoft.admin.controller;

import com.banksoft.admin.bean.OrderStatus;
import com.banksoft.admin.bean.Status;
import com.banksoft.admin.dao.OrderDao;
import com.banksoft.admin.dao.ProductDao;
import com.banksoft.admin.dao.ProductParamDao;
import com.banksoft.admin.entity.*;
import com.banksoft.admin.service.BaseService;
import com.banksoft.admin.service.OrderProductService;
import com.banksoft.admin.service.OrderService;
import com.banksoft.admin.service.ProductService;
import com.banksoft.common.BaseController;
import com.banksoft.common.PageInfo;
import com.banksoft.util.DateUtil;
import com.banksoft.util.StringNumber;
import com.banksoft.util.StringUtil;
import com.banksoft.util.UUIDFactory;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by vanjoger on 2016/12/27.
 */
@Controller
@RequestMapping("/product")
public class ProductController extends BaseController{

    @Autowired
    BaseService baseService;
    @Autowired
    ProductService productService;
    @Autowired
    OrderService orderService;
    @Autowired
    OrderProductService orderProductService;


    @RequestMapping("/list")
    public String list(){
        return "product/product_list";
    }

    @RequestMapping("/listData")
    @ResponseBody
    public Object listData(String name,String status,String startDate,String endDate,PageInfo pageInfo){
        Map<String,Object> map = new HashMap<>();
        if(StringUtil.isNotNull(name))
            map.put("name",name);
        if(StringUtil.isNotNull(status))
            map.put("status",status);
        if(StringUtil.isNotNull(startDate))
            map.put("startDate", DateUtil.getLongFormStrDate(startDate));
        if(StringUtil.isNotNull(endDate))
            map.put("endDate",DateUtil.getLongFormStrDate(endDate));
        pageInfo.setParams(map);

        pageInfo = this.page(ProductDao.class,pageInfo);

        return this.getPageResult(pageInfo);
    }


    @RequestMapping("/beforeEdit")
    public String beforeEdit(String id,Model model){
        if(StringUtil.isNotNull(id)){
            Product product = this.baseService.findById(ProductDao.class,id);
            model.addAttribute("data",product);
            ProductParam param = this.baseService.findById(ProductParamDao.class,id);
            model.addAttribute("productParam",param);
        }
        return "product/product_edit";
    }

    @RequestMapping("/save")
    @ResponseBody
    public JSONObject save(Product product,String memberIn,String goldIn,String goldOut,String generalIn,String generalOut,String generalGoldIn,String generalGoldOut,String partnerIn,String partnerOut,HttpServletRequest request){
        try {
            JSONObject json = this.productService.save(product,goldIn,goldOut,generalIn,generalOut,partnerIn,partnerOut,generalGoldIn,generalGoldOut,memberIn);
            if(json.getBoolean("success")){
                if(StringUtil.isNotNull(product.getId())) {
                    this.saveLog("编辑产品" + product.getName(), request);
                }else{
                    this.saveLog("新增产品" +product.getName(), request);
                }
            }
            return json;
        }catch (Exception e){
            e.printStackTrace();
            return this.FmtResult(false,"保存失败",null);
        }

    }

    @RequestMapping("/updateStatus")
    @ResponseBody
    public Object updateStatus(String id,String status,HttpServletRequest request){
        try{
            Product product = this.baseService.findById(ProductDao.class,id);
            if(product!=null){
                product.setStatus(status);
                this.baseService.update(ProductDao.class,product);
                if(status.equals(Status.NORMAL.name())){
                    this.saveLog("上架产品"+product.getName(),request);
                }else{
                    this.saveLog("下架产品"+product.getName(),request);
                }
                return this.FmtResult(true,"操作成功",null);
            }else{
                return this.FmtResult(false,"没有找到数据",null);
            }
        }catch (Exception e){
            e.printStackTrace();
            return this.FmtResult(false,"操作失败"+e.getMessage(),null);
        }
    }



    @RequestMapping("/count")
    public String count(){
        return "product/product_count";
    }


    @RequestMapping("/queryProductData")
    @ResponseBody
    public JSONObject listData(String startDate, String endDate){
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("status", new String[]{OrderStatus.Pay.name(), OrderStatus.Send.name(), OrderStatus.Suc.name()});
            if (StringUtil.isNotNull(startDate)) {
                startDate += " 00:00:00";
                map.put("startDate", DateUtil.getLongFormStrDate(startDate));
            }
            if (StringUtil.isNotNull(endDate)) {
                endDate += " 23:59:59";
                map.put("endDate", DateUtil.getLongFormStrDate(endDate));
            }
            List<Order> list = orderService.query(map);

            List<Product> productList = this.baseService.findAll(ProductDao.class);

            if (StringUtil.isNotEmpty(productList)) {
                for (Product product : productList) {
                    String num = "0";
                    for (Order order : list) {
                        List<OrderProduct> orderProductList = this.orderProductService.listByOrderId(order.getId());
                        for (OrderProduct op : orderProductList) {
                            if(op.getProductId().equals(product.getId()))
                                num = StringNumber.add(op.getNum(), num);
                        }
                    }
                    product.setNum(num);
                }
            }
            return this.FmtResult(true, null, productList);
        }catch (Exception e){
            e.printStackTrace();
        }
        return this.FmtResult(false,null,null);
    }

}
